package com.ssh.service.impl;

import java.util.List;

import org.springframework.transaction.annotation.Transactional;

import com.ssh.dao.EmpDao;
import com.ssh.entity.Emp;
import com.ssh.entity.PageBean;
import com.ssh.service.EmpService;

@Transactional
public class EmpServiceImpl implements EmpService {
	//注入Empdao
	private EmpDao empDao;
	public void setEmpDao(EmpDao empDao) {
		this.empDao = empDao;
	}
	//添加
	@Override
	public void add(Emp emp) {
		
		empDao.add(emp);
	}
	//查询
	@Override
	public List<Emp> findAll() {
		
		return empDao.findAll();
	}
	//根据用户名查询
	@Override
	public Emp fingByName(String eusername) {
		
		return empDao.findByName(eusername);
	}
	
	//根据id查询
	@Override
	public Emp findById(int eid) {
		return empDao.findById(eid);
	}
	//删除
	@Override
	public void delete(Emp emp) {
		
		empDao.delete(emp);
	}
	//修改
	@Override
	public void update(Emp emp) {
		
		empDao.update(emp);
	}
	//分页
	@Override
	public PageBean<Emp> listPage(Integer currenPage) {
		//pageBean
		PageBean<Emp> pageBean=new PageBean<Emp>();
		//当前页
		pageBean.setCurrenPage(currenPage);
		//每页显示记录数
		int pageSize=5;
		//总记录数
		int totalCount=empDao.findCount();
		pageBean.setTotalCount(totalCount);
		System.out.println(totalCount);
		//总页数
		int totalPage=0;
		if(totalCount%pageSize==0){//整数
			totalPage=totalCount%pageSize;
			System.out.println(totalPage);
		}else{//+1
			totalPage=totalCount%pageSize+1;
		}
		pageBean.setTotalPage(totalPage);
		//第几页开始
		int begin=(currenPage-1)*pageSize;
		//每页的数据
		List<Emp> list=empDao.findPage(begin,pageSize);
		pageBean.setList(list);
		return pageBean;
	}

}
